package com.itheima.reggie.service.impl;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.*;

@Service
@Slf4j
public class StaticsServiceImpl implements StaticsService {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private DishService dishService;

    @Autowired
    private SetMealService setmealService;

    /*计算菜系数据*/
    @Override
    public Statics getDishCategoryCash() {
        List<Cash> dishCategoryCash = new ArrayList<>();
        //查询订单的id
        List<OrderDetail> orderDetailList = orderDetailService.list();
        //dishId集合
        Map<String, Long> dishMap = new HashMap<>();
        for (OrderDetail orderDetail : orderDetailList) {
            //菜品
            if (orderDetail.getDishId() != null) {
                //获取dish
                Dish dish = dishService.getById(orderDetail.getDishId());
                //获取菜品价格
                BigDecimal price = dish.getPrice();
                Long priceLong = price.longValue();
                Category category = categoryService.getById(dish.getCategoryId());
                if (dishMap.get(category.getName()) == null) {
                    dishMap.put(category.getName(), priceLong);
                } else {
                    dishMap.put(category.getName(), dishMap.get(category.getName()) + priceLong);
                }
            }
        }
        log.info("DishMap为:" + dishMap);
        //dishMap遍历录入
        Set<Map.Entry<String, Long>> entries = dishMap.entrySet();
        for (Map.Entry<String, Long> entry : entries) {
            Cash cash = new Cash();
            cash.setName(entry.getKey());
            cash.setValue(entry.getValue());
            dishCategoryCash.add(cash);
        }
        Statics statics = new Statics();
        statics.setDishCategoryCash(dishCategoryCash);
        return statics;
    }




    @Override
    public Statics getSetmealCategoryCash() {
        List<Cash> setmealCash = new ArrayList<>();
        List<OrderDetail> orderDetailList = orderDetailService.list();
        Map<String, Long> setmealMap = new HashMap<>();
        for (OrderDetail orderDetail : orderDetailList) {
            if (orderDetail.getSetmealId() != null) {
                //套餐
                Setmeal setmeal = setmealService.getById(orderDetail.getSetmealId());
                //获取套餐价格
                BigDecimal price = setmeal.getPrice();
                Long priceLong = price.longValue();

                Category category = categoryService.getById(setmeal.getCategoryId());
                if (setmealMap.get(category.getName()) == null) {
                    setmealMap.put(category.getName(), priceLong);
                } else {
                    setmealMap.put(category.getName(), setmealMap.get(category.getName()) + priceLong);
                }
            }
        }
        log.info("SetmealMap为:" + setmealMap);
        //setmealMap遍历录入
        Set<Map.Entry<String, Long>> entries1 = setmealMap.entrySet();
        for (Map.Entry<String, Long> entry1 : entries1) {
            Cash cash = new Cash();
            cash.setName(entry1.getKey());
            cash.setValue(entry1.getValue());
            setmealCash.add(cash);
        }
        Statics statics = new Statics();
        statics.setSetmealCash(setmealCash);
        return statics;
    }



    /*菜品销量占比*/
    @Override
    public Statics getDishNum() {
        List<Cash> dishNum = new ArrayList<>();
        List<OrderDetail> orderDetailList = orderDetailService.list();
        //dishId集合
        Map<String, Integer> dishMap = new HashMap<>();
        for (OrderDetail orderDetail : orderDetailList) {
            //菜品
            if (orderDetail.getDishId() != null) {
                //获取dish
                Dish dish = dishService.getById(orderDetail.getDishId());
                //从dish通过CategoryId获取category
                Category category = categoryService.getById(dish.getCategoryId());
                if (dishMap.get(category.getName()) == null) {
                    dishMap.put(category.getName(), 1);
                } else {
                    dishMap.put(category.getName(), dishMap.get(category.getName()) + 1);
                }
            }
        }
        log.info("DishMap为:" + dishMap);
        //dishMap遍历录入
        Set<Map.Entry<String, Integer>> entries = dishMap.entrySet();
        for (Map.Entry<String, Integer> entry : entries) {
            Cash cash = new Cash();
            cash.setName(entry.getKey());
            cash.setValue(Long.valueOf(entry.getValue()));
            dishNum.add(cash);
        }
        Statics statics = new Statics();
        statics.setDishNum(dishNum);
        return statics;
    }



    @Override
    public Statics getSetmealNum() {
        List<Cash> setmealNum = new ArrayList<>();
        //查询订单的id
        List<OrderDetail> orderDetailList = orderDetailService.list();
        //查询 id
        Map<String, Integer> setmealMap = new HashMap<>();
        for (OrderDetail orderDetail : orderDetailList) {
            if (orderDetail.getSetmealId() != null) {
                //套餐
                Setmeal setmeal = setmealService.getById(orderDetail.getSetmealId());
                Category category = categoryService.getById(setmeal.getCategoryId());
                if (setmealMap.get(category.getName()) == null) {
                    setmealMap.put(category.getName(), 1);
                } else {
                    setmealMap.put(category.getName(), setmealMap.get(category.getName()) + 1);
                }
            }
        }
        log.info("SetmealMap为:" + setmealMap);
        //setmealMap遍历录入
        Set<Map.Entry<String, Integer>> entries1 = setmealMap.entrySet();
        for (Map.Entry<String, Integer> entry1 : entries1) {
            Cash cash = new Cash();
            cash.setName(entry1.getKey());
            cash.setValue(Long.valueOf(entry1.getValue()));
            setmealNum.add(cash);
        }
        Statics statics = new Statics();
        statics.setSetmealNum(setmealNum);
        return statics;
    }
}

